package org.krishna.File;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.krishna.jaxb.xml.Course;

public class ExcelData {
	
	static HSSFWorkbook workbook = new HSSFWorkbook();
	static HSSFSheet sheet = workbook.createSheet("Report sheet");
	public static void writeDataToExcel(String studentId, String studentName, List<Course> courses, String folderName){
		int rowNum = 0;
		int CellNum = 0;
		Row row = sheet.createRow(rowNum++);
		Cell cell = row.createCell(CellNum++);
		cell.setCellValue("Report");
		row = sheet.createRow(rowNum++);
		CellNum = 0;
		cell = row.createCell(CellNum++);
		cell.setCellValue("Course Name");
		cell = row.createCell(CellNum++);
		cell.setCellValue("Max Marks");
		cell = row.createCell(CellNum++);
		cell.setCellValue("Max Scored");
		cell = row.createCell(CellNum++);
		cell.setCellValue("Result");
			
		for(Course course: courses){
			row = sheet.createRow(rowNum++);	
			CellNum = 0;			
			cell = row.createCell(CellNum++);
			cell.setCellValue(course.getName());
			cell = row.createCell(CellNum++);
			cell.setCellValue(course.getMaxmarks());
			cell = row.createCell(CellNum++);
			cell.setCellValue(course.getMarksobtained());
			cell = row.createCell(CellNum++);
			cell.setCellValue(course.getResult());
			
		}
		
		try {
		    FileOutputStream out = new FileOutputStream(new File(folderName+"\\"+studentName+".xls"));
		    workbook.write(out);
		    out.close();
		    System.out.println("Excel written successfully..");
		     
		} catch (FileNotFoundException e) {
		    e.printStackTrace();
		} catch (IOException e) {
		    e.printStackTrace();
		}
		
	}

}
